import { request, downloadBlobFile } from 'src/utils';

export interface WorkProcessDto {
  processId: IdType; // 处理过程ID
  orderId: string; // 工单id
  content: string; // 处理内容描述
  createdBy: string; // 创建人
  createdTime: Date; // 创建时间
}

const baseUrl = '/api/workorder/workProcess';

/**
 * 查单个
 */
export function readData(processId: IdType): Promise<WorkProcessDto> {
  return request.get(baseUrl + '/' + processId);
}

/**
 * 查集合
 */
export function queryList(query: PageQuery): Promise<PageResult<WorkProcessDto>> {
  return request.get(baseUrl, { params: query });
}

/**
 * 新增，修改
 */
export function saveOrUpdate(workProcess: WorkProcessDto): Promise<WorkProcessDto> {
  return workProcess.processId ? request.put(baseUrl + '/' + workProcess.processId, workProcess) : request.post(baseUrl, workProcess);
}

/**
 * 删除
 */
export function deleteByIds(ids: IdType[]): Promise<void> {
  return request.delete(baseUrl, { params: { ids } });
}

/**
 * 导出
 */
export function exportData(query: PageQuery): Promise<void> {
  return new Promise((resolve, reject) => {
    request
      .get(baseUrl + '/export', { params: query, responseType: 'blob' })
      .then((res) => {
        downloadBlobFile(res);
        resolve();
      })
      .catch((err) => reject(err));
  });
}

export default { readData, queryList, saveOrUpdate, deleteByIds, exportData } as CrudApi<WorkProcessDto>;
